package com.tanhua.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.dubbo.mappers.UserMapper;
import com.tanhua.model.db.User;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;


@DubboService
public class UserApiImpl implements UserApi {

    @Autowired
    private UserMapper userMapper;

    /**
     * 保存用户
     */
    @Override
    public Long save(User user) {
         userMapper.insert(user);
        System.out.println("添加的用户id："+ user.getId());
        return user.getId();
    }

   //根据 id查询用户
    public User findById(Long userId) {
        QueryWrapper<User> qw = new QueryWrapper<>();
        qw.eq("id",userId);
        User user = userMapper.selectOne(qw);
        return user;
    }

    @Override
    public void updatePhone(User user) {
        userMapper.updateById(user);
    }

   //更新用户
    public void update(User user) {
        userMapper.updateById(user);
    }

   //查询全部
    public List<User> findAll() {
        return userMapper.selectList(null);
    }


    /**
     * 根据手机号码查询用户
     */
    public User findByMobile(String mobile) {
        //SELECT * FROM `tb_user` where mobile='13800138000'
        QueryWrapper<User> qw = new QueryWrapper<>();
        qw.eq("mobile",mobile);
        return userMapper.selectOne(qw);
    }

}
